package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCommentMapper;
import com.qf.pojo.DtsComment;
import com.qf.service.DtsCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DtsCommentServiceImpl implements DtsCommentService {
    @Autowired
    DtsCommentMapper commentMapper;
    @Override
    public Map<String, Object> Page(Integer page, Integer limit, Integer userId, Integer valueId, String sort, String order, List<Integer> brandIds) {
        IPage<DtsComment> ipage = new Page<>(page,limit);
        QueryWrapper<DtsComment> wrapper = new QueryWrapper<>();
        wrapper.eq("c.deleted", false);

        if (userId != null) {
            wrapper.eq("c.user_id", userId);
        }

        if (valueId != null) {
            wrapper.eq("c.value_id", valueId);
        }

        if (brandIds != null && brandIds.size() > 0) {
            wrapper.in("g.brand_id", brandIds);
        }


        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if ("desc".equals(order)) {
                wrapper.orderByDesc(sort);
            } else {
                wrapper.orderByAsc(sort);
            }
        }

        ipage = commentMapper.selectByPage(ipage, wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("total", ipage.getTotal());
        data.put("items", ipage.getRecords());

        return data;
    }

    @Override
    public void delete(Integer id) {
        commentMapper.deleteById(id);
    }
}